import allure
from selenium.webdriver.common.by import By

from base.base_page import BasePage
from testcases.conftest import delete_code
from utils.assert_util import assert_compare


class UserPage(BasePage):
    # 请登录
    ple_login = (By.CSS_SELECTOR, 'a[href="#/app/login"]')
    # 会员中心
    vip_center = (By.XPATH, '//a[contains(text(),"会员中心")]')
    # 我的订单
    my_order = (By.CSS_SELECTOR, 'a[href="#/app/home/member/order"]')
    # 我的收藏
    my_fav = (By.CSS_SELECTOR, 'a[href="#/app/home/member/collection"]')
    # 我的收货地址
    my_address = (By.CSS_SELECTOR, 'a[href="#/app/home/member/receive"]')
    # 购物车
    shopping_cart = (By.CSS_SELECTOR, 'a[href="#/app/shoppingcart/cart"]')
    # 免费注册
    register = (By.CSS_SELECTOR, 'a[href="#/app/register"]')
    # 手机号
    register_mobile = (By.ID, 'jsRegMobile')
    # 免费获取验证码
    send_code = (By.ID, 'jsSendCode')
    # 输入手机验证码
    input_code = (By.ID, 'jsPhoneRegCaptcha')
    # 输入密码
    input_password = (By.ID, 'jsPhoneRegPwd')
    # 注册并登录
    register_sumbit = (By.ID, 'jsMobileRegBtn')
    # 退出登录
    login_out = (By.XPATH, '//*[@id="ECS_MEMBERZONE"]/a[2]')

    # 用户名
    user_name = (By.CSS_SELECTOR, 'a[href="#/app/home/member/userinfo"]')
    # 验证码错误
    error_code = (By.XPATH, '//*[@id="mobile_register_form"]/p[2]')
    # 用户已存在
    user_exit = (By.XPATH, '//*[@id="mobile_register_form"]/p[1]')
    """
    登录相关
    """
    # 用户名
    login_account = (By.XPATH, '//*[@id="account_l"]')
    # 密码
    login_password = (By.XPATH, '//*[@id="password_l"]')
    # 立即登录按钮
    login_button = (By.XPATH, '//*[@id="jsLoginBtn"]')
    # 登录用户名为空
    login_account_error = (By.XPATH, '//*[@id="jsLoginForm"]/p[1]')
    # 登录密码为空
    login_password_error = (By.XPATH, '//*[@id="jsLoginForm"]/p[2]')
    # 登录错误密码登录
    login_error = (By.XPATH, '//*[@id="jsLoginForm"]/p[3]')

    """收货信息"""
    # 配送区域
    express_area = (By.XPATH, "//td[contains(text(),'配送区域')]")
    # 市
    province = (By.XPATH, "//td[contains(text(),'配送区域')]/following-sibling::td/div/div/select")
    # 市
    city = (By.XPATH, "//td[contains(text(),'配送区域')]/following-sibling::td/div/div/select[2]")
    # 区
    district = (By.XPATH, "//td[contains(text(),'配送区域')]/following-sibling::td/div/div/select[3]")
    # 收货人姓名
    username = (By.XPATH, '//*[@id="consignee_0"]')
    # 详细地址
    detailed_address = (By.XPATH, '//*[@id="address_0"]')
    # 手机
    mobile = (By.XPATH, '//*[@id="mobile_0"]')
    # 新增收货地址按钮
    add_address_button = (By.XPATH, "//button[contains(text(),'新增收货地址')]")

    # 确定修改按钮
    confirm_edit = (By.XPATH, "//button[contains(text(),'确定修改')]")
    # 删除
    delete_address = (By.XPATH, "//button[contains(text(),'删除')]")

    def register_failed(self, data):
        """
        注册失败
        :return:
        """
        # 打开首页
        with allure.step("打开首页"):
            self.get_url("http://meikefresh.5istudy.online/")
        with allure.step("点击注册按钮"):
            self.click(self.register)
        with allure.step("输入手机号"):
            self.send_keys(self.register_mobile, data['mobile'])
        with allure.step("点击获取验证码"):
            self.click(self.send_code)
        with allure.step("点击输入验证码"):
            self.send_keys(self.input_code, "f123")
        with allure.step("点击输入密码"):
            self.send_keys(self.input_password, data['password'])
        with allure.step("点击注册并登录"):
            self.click(self.register_sumbit)
        text = self.get_text(self.error_code)
        assert_compare("验证码错误", "==", text)

    def login(self):
        """登录"""
        self.get_url("http://meikefresh.5istudy.online/#/app/login")
        self.send_keys(self.login_account, "18339914656")
        self.send_keys(self.login_password, "123456")
        self.click(self.login_button)
